<template>
	<view class="content">
		<view>方法集---------------</view>
		<button class="size" @tap="openBluetoothAdapter" size="mini">初始化蓝牙模块</button>
		<button class="size" @tap="closeBluetoothAdapter" size="mini">关闭蓝牙模块</button>
		<button class="size" @tap="getBluetoothAdapterState" size="mini">获取本机蓝牙适配器状态</button>
		<button class="size" @tap="getBluetoothDevices" size="mini">获取已搜索到的蓝牙设备</button>
		<button class="size" @tap="getConnectedBluetoothDevices" size="mini">根据uuid获取处于已连接的设备</button>
		<button class="size" @tap="listenerStateChange" size="mini">监听蓝牙适配器状态变化事件</button>
		<button class="size" @tap="onBluetoothDeviceFound" size="mini">监听搜索到新设备的事件</button>
		<button class="size" @tap="startBluetoothDevicesDiscovery" size="mini">开始搜索附近的蓝牙设备 </button>
		<button class="size" @tap="stopBluetoothDevicesDiscovery" size="mini">停止搜寻附近的蓝牙外围设备 </button>
		<button class="size" @tap="closeBLEConnection" size="mini">断开与低功耗蓝牙设备的连接 </button>
		<button class="size" @tap="createBLEConnection" size="mini">连接低功耗蓝牙设备 </button>
		<button class="size" @tap="getBLEDeviceRSSI" size="mini">获取蓝牙设备的信号强度 </button>
		<button class="size" @tap="getBLEDeviceServices" size="mini">获取蓝牙设备的所有服务(service) </button>
		<button class="size" @tap="notifyBLECharacteristicValueChange"
			size="mini">启用低功耗蓝牙设备特征值变化时的notify功能，订阅特征值</button>
		<button class="size" @tap="onBLECharacteristicValueChange" size="mini">监听低功耗蓝牙设备的特征值变化事件 </button>
		<button class="size" @tap="onBLEConnectionStateChange" size="mini">监听低功耗蓝牙设备连接状态变化事件 </button>
		<button class="size" @tap="readBLECharacteristicValue" size="mini">读取低功耗蓝牙设备指定特征值的二进制数据值
		</button>
		<button class="size" @tap="writeBLECharacteristicValue" size="mini">向低功耗蓝牙设备指定特征值写入二进制数据</button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				deviceId: "",
				serviceId: "",
				characteristicId: ''
			}
		},
		methods: {
			closeBluetoothAdapter() {
				plus.bluetooth.closeBluetoothAdapter({
					success: (e) => {
						Object.keys(e).forEach(key => {
							console.log("success=>", key, e[key]);
						})
					},
					fail: (e) => {
						Object.keys(e).forEach(key => {
							console.log("fail=>", key, e[key]);
						})
					}
				});
			},
			openBluetoothAdapter() {
				// 用来打开蓝牙
				plus.bluetooth.openBluetoothAdapter({
					success(e) {
						Object.keys(e).forEach(key => {
							console.log("success=>", key, e[key]);
						})
					},
					fail(e) {
						Object.keys(e).forEach(key => {
							console.log("fail=>", key, e[key]);
						})
					}
				});
			},
			getBluetoothAdapterState() {
				// 蓝牙开启状态=>success,关闭状态 => fail，手机蓝牙开启关闭状态监听
				plus.bluetooth.getBluetoothAdapterState({
					success: (e) => {
						Object.keys(e).forEach(key => {
							console.log("success=>", key, e[key]);
						})
					},
					fail: (e) => {
						Object.keys(e).forEach(key => {
							console.log("fail=>", key, e[key]);
						})
					}
				});
			},
			getBluetoothDevices() {
				// 获取已搜索到的蓝牙设备
				plus.bluetooth.getBluetoothDevices({
					success: (e) => {
						var devices = e.devices;
						console.log(devices,"devices===")
						console.log('get devices success: ' + devices.length);
						for (var i in devices) {
							console.log(i + ': ' + JSON.stringify(devices[i]));
						}
					},
					fail: (e) => {
						Object.keys(e).forEach(key => {
							console.log("fail=>", key, e[key]);
						})
					}
				});
			},
			getConnectedBluetoothDevices() {
				plus.bluetooth.getConnectedBluetoothDevices({
					success: function(e) {
						var devices = e.devices;
						console.log('connected devices success: ' + e.length);
						for (var i in devices) {
							console.log(i + ': ' + JSON.stringify(devices[i]));
						}
					},
					fail: function(e) {
						console.log('connected devices failed: ' + JSON.stringify(e));
					}
				});
			},
			listenerStateChange() {
				plus.bluetooth.onBluetoothAdapterStateChange(function(e) {
					console.log('state changed: ' + JSON.stringify(e));
				});
			},
			onBluetoothDeviceFound() {
				plus.bluetooth.onBluetoothDeviceFound(function(e) {
					var devices = e.devices;
					console.log('device found: ' + e.length);
					for (var i in devices) {
						console.log(i + ': ' + JSON.stringify(devices[i]));
					}
				});
			},
			startBluetoothDevicesDiscovery() {
				plus.bluetooth.openBluetoothAdapter({
					success: function(e) {
						console.log('open success: ' + JSON.stringify(e));
						plus.bluetooth.startBluetoothDevicesDiscovery({
							success: function(e) {
								console.log('start discovery success: ' + JSON.stringify(e));
							},
							fail: function(e) {
								console.log('start discovery failed: ' + JSON.stringify(e));
							}
						});
					},
					fail: function(e) {
						console.log('open failed: ' + JSON.stringify(e));
					}
				});
			},
			stopBluetoothDevicesDiscovery() {
				plus.bluetooth.stopBluetoothDevicesDiscovery({
					success: function(e) {
						console.log('stop discovery success: ' + JSON.stringify(e));
						plus.bluetooth.closeBluetoothAdapter({
							success: function(e) {
								console.log('close success: ' + JSON.stringify(e));
							},
							fail: function(e) {
								console.log('close failed: ' + JSON.stringify(e));
							}
						});
					},
					fail: function(e) {
						console.log('stop discovery failed: ' + JSON.stringify(e));
					}
				})
			},
			closeBLEConnection() {
				plus.bluetooth.closeBLEConnection({
					deviceId: this.deviceId,
					success: function(e) {
						console.log('close success: ' + JSON.stringify(e));
					},
					fail: function(e) {
						console.log('close failed: ' + JSON.stringify(e));
					}
				});
			},
			createBLEConnection() {
				plus.bluetooth.createBLEConnection({
					deviceId: this.deviceId,
					success: function(e) {
						console.log('create connection success: ' + JSON.stringify(e));
					},
					fail: function(e) {
						console.log('create connection failed: ' + JSON.stringify(e));
					}
				});
			},
			getBLEDeviceRSSI() {
				plus.bluetooth.createBLEConnection({
					deviceId: this.deviceId,
					success: function(e) {
						console.log('create connection success: ' + JSON.stringify(e));
						plus.bluetooth.getBLEDeviceRSSI({
							deviceId: this.deviceId,
							success: function(e) {
								console.log('RSSI: ' + e.RSSI);
							},
							fail: function(e) {
								console.log('get RSSI failed: ' + JSON.stringify(e));
							}
						});
					},
					fail: function(e) {
						console.log('create connection failed: ' + JSON.stringify(e));
					}
				});
			},
			getBLEDeviceServices() {
				plus.bluetooth.getBLEDeviceServices({
					deviceId: this.deviceId,
					success: function(e) {
						var services = e.services;
						console.log('get services success: ' + services.length);
						for (var i in services) {
							console.log(i + ': ' + JSON.stringify(services[i]));
						}
					},
					fail: function(e) {
						console.log('get services failed: ' + JSON.stringify(e));
					}
				});
			},
			notifyBLECharacteristicValueChange() {
				plus.bluetooth.onBLECharacteristicValueChange(function(e) {
					console.log('onBLECharacteristicValueChange: ' + JSON.stringify(e));
					var value = buffer2hex(e.value);
					console.log('value(hex) = ' + value);
					if (characteristicId == e.characteristicId) {
						// 更新到页面显示
						alert('特征值变化: ' + value);
					}
				});
				// 启用notify功能
				plus.bluetooth.notifyBLECharacteristicValueChange({
					deviceId: this.deviceId,
					serviceId: this.serviceId,
					characteristicId: this.characteristicId,
					success: function(e) {
						var characteristics = e.characteristics;
						console.log('get characteristics success: ' + characteristics.length);
						for (var i in characteristics) {
							console.log(i + ': ' + JSON.stringify(characteristics[i]));
						}
					},
					fail: function(e) {
						console.log('get characteristics failed: ' + JSON.stringify(e));
					}
				});
			},
			onBLECharacteristicValueChange() {
				plus.bluetooth.onBLECharacteristicValueChange(function(e) {
					console.log('onBLECharacteristicValueChange: ' + JSON.stringify(e));
					var value = buffer2hex(e.value);
					console.log('value(hex) = ' + value);
					if (characteristicId == e.characteristicId) {
						// 更新到页面显示
						alert('特征值变化: ' + value);
					}
				});
				// 启用notify功能
				plus.bluetooth.notifyBLECharacteristicValueChange({
					deviceId: this.deviceId,
					serviceId: this.serviceId,
					characteristicId: characteristicId,
					success: function(e) {
						var characteristics = e.characteristics;
						console.log('get characteristics success: ' + characteristics.length);
						for (var i in characteristics) {
							console.log(i + ': ' + JSON.stringify(characteristics[i]));
						}
					},
					fail: function(e) {
						console.log('get characteristics failed: ' + JSON.stringify(e));
					}
				});
			},
			onBLEConnectionStateChange() {
				plus.bluetooth.onBLEConnectionStateChange(function(e) {
					console.log('connection state changed: ' + JSON.stringify(e));
				});
			},
			readBLECharacteristicValue() {
				plus.bluetooth.readBLECharacteristicValue({
					deviceId: this.deviceId,
					serviceId: this.serviceId,
					characteristicId: this.characteristicId,
					success: function(e) {
						console.log('read characteristics success: ' + JSON.stringify(e));
					},
					fail: function(e) {
						console.log('read characteristics failed: ' + JSON.stringify(e));
					}
				});
			},
			writeBLECharacteristicValue() {
				// 要写入的数据
				var value = new ArrayBuffer(8);
				var iv = new Int32Array(value);
				iv[0] = 120, iv[2]=100;
				plus.bluetooth.writeBLECharacteristicValue({
					deviceId: this.deviceId,
					serviceId: this.serviceId,
					characteristicId: this.characteristicId,
					value: value,
					success: function(e) {
						console.log('write characteristics success: ' + JSON.stringify(e));
					},
					fail: function(e) {
						console.log('write characteristics failed: ' + JSON.stringify(e));
					}
				});
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 14px;
		color: #8f8f94;
	}

	.size {
		margin: 20rpx 0;
	}

	.fixed {
		position: fixed;
		top: 20%;
		left: 40rpx;
		color: red;
		font-weight: 500;
	}

	#bcid {
		background: #0F0;
		height: 300px;
		width: 280px;
	}
</style>
